package com.huike.utils;

import cn.hutool.core.date.DateUtil;
import com.huike.dto.domain.AssignRecord;
import com.huike.dto.domain.RulePool;
import com.huike.service.IRulePoolService;
import com.huike.utils.spring.SpringUtils;

import java.util.Calendar;
import java.util.Date;

/**
 * 获取结束时间的工具类
 *
 */
public class HuiKeCrmDateUtils {
    /**
     * 根据规则获取结束时间
     * @return
     */
    public static Date getEndDateByRule(AssignRecord assignRecord){
        IRulePoolService rulePoolService = SpringUtils.getBean(IRulePoolService.class);
        RulePool rulePool= rulePoolService.selectRulePoolByType(assignRecord.getType());
        if(rulePool==null){
            return null;
        }
        //回收规则
        Date recoveryDate=getDate(rulePool.getLimitTime().intValue(),
                rulePool.getLimitTimeType(), DateUtil.date(assignRecord.getCreateTime()));
        return recoveryDate;
    }

    public static Date getDate(int time, String type, Date now){
        Calendar cal = Calendar.getInstance();
        cal.setTime(now);
        if(RulePool.LimitTimeType.HOUR.getValue().equals(type)){
            cal.add(Calendar.HOUR, time);
        }else if(RulePool.LimitTimeType.DAY.getValue().equals(type)){
            cal.add(Calendar.DATE, time);
        }else if(RulePool.LimitTimeType.WEEK.getValue().equals(type)){
            cal.add(Calendar.DAY_OF_WEEK, time);
        }
        return cal.getTime();
    }
}
